﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace Radar
{
    public partial class Form1 : Form
    {
        public const int WmNclbuttondown = 0xA1;
        public const int HtCaption = 0x2;
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        private readonly Pen _pen1 = new Pen(Color.Green, 1);
        //private readonly Pen _pen2 = new Pen(Color.LightGreen, 10);
        //private readonly Pen _pen2 = new Pen(Color.Red, 10);
        private readonly Pen _pen3 = new Pen(Color.Honeydew, 1);
        private readonly Brush _brush1 = new SolidBrush(Color.Black);
        private readonly Brush _brush2 = new SolidBrush(Color.GreenYellow);
        private readonly Brush _brush3 = new SolidBrush(Color.White);
        //private readonly Brush _brush3 = new SolidBrush(Color.Red);
        private Thread _radar;
        private Double _angle;
        private Double _shadowAngle;
        private Point _centerPoint = new Point(199, 199);
        private Point _point1 = new Point(200, 0);
        private Point[] _enemies =
        {
            new Point(155,155),
            new Point(100,300),
             new Point(100,170)  
        };

        public Form1()
        {
            InitializeComponent();
            TransparencyKey = BackColor;
            Opacity = 0.85;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _radar = new Thread(Radar);
            _radar.IsBackground = true;
            _radar.Start();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WmNclbuttondown, HtCaption, 0);
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            //back
           e.Graphics.FillEllipse(_brush1, 0, 0, 400, 400);
            // rings
            for (int i = 0; i < 5; i++)
            {
                e.Graphics.DrawEllipse(_pen1, i * 40 + 1, i * 40 + 1, 398 - i * 80, 398 - i * 80);
            }
            // hand
            _point1.X = (int)(Math.Sin(Math.PI / 3 * _angle) * 200) + 200;
            _point1.Y = (int)(Math.Cos(Math.PI / 3 * _angle) * 200) + 200;
            //e.Graphics.DrawLine(_pen2, 199, 199, _point1.X, _point1.Y);
            // hand shadow
            _shadowAngle = _angle;
            for (int i = 1; i < 50; i++)
            {
                _shadowAngle = _shadowAngle - 0.005;
                _point1.X = (int)(Math.Sin(Math.PI / 3 * _shadowAngle) * 200) + 200;
                _point1.Y = (int)(Math.Cos(Math.PI / 3 * _shadowAngle) * 200) + 200;
                e.Graphics.DrawLine(_pen3, 199, 199, _point1.X, _point1.Y);
            }
            // center
            //e.Graphics.FillEllipse(_brush2, 189, 189, 20, 20);
            // enemies
            foreach(Point enemy in _enemies )
            {
                if (IsPointOnLine(_centerPoint, _point1, enemy))
                {
                    e.Graphics.FillEllipse(_brush3,enemy.X-5,enemy.Y-5,10,10);
                }
            }
        }

        private void Radar()
        {
            while (true)
            {
                _angle = _angle + 0.1;
                if (_angle > 6) _angle = 0;
                Invalidate();
                Thread.Sleep(60);
            }
        }
        private const float Epsilon = 30f;
        private bool IsPointOnLine(Point linePointA, Point linePointB, Point point)
        {
            float a = (linePointB.Y - linePointA.Y) / (linePointB.X - linePointA.X);
            float b = linePointA.Y - a * linePointA.X;
            if (Math.Abs(point.Y - (a * point.X + b)) <= Epsilon)
            {
                int lo_x = (linePointA.X < linePointB.X) ? linePointA.X : linePointB.X;
                int hi_x = (linePointA.X > linePointB.X) ? linePointA.X : linePointB.X;
                int lo_y = (linePointA.Y < linePointB.Y) ? linePointA.Y : linePointB.Y;
                int hi_y = (linePointA.Y > linePointB.Y) ? linePointA.Y : linePointB.Y;
                if (point.X < lo_x) return false;
                if (point.X > hi_x) return false;
                if (point.Y < lo_y) return false;
                if (point.Y > hi_y) return false;
                return true;
            }
            return false;
        }
    }
}
